package tmi.li.txwidgets.txclck;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.IBinder;
import android.os.Build.VERSION;
import android.util.Log;
import android.widget.RemoteViews;


import tmi.li.txwidgets.R;
import tmi.li.txwidgets.TxWidgetsFinals;


public class TxClckProvider extends AppWidgetProvider {
	private static final String TAG = "tmi.li.txwidgets.txclck.TxClckProvider";

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {	
		TxClckWidget widget = new TxClckWidget(context);
		for(int id : appWidgetIds) {
			if(widget.load(id)) {
				Log.d(TAG, "onUpdate() id => "+id);
				updateWidget(context, appWidgetManager, id);
			}
		}
	}

	@Override
    public void onEnabled(Context context) {
        Log.d(TAG, "onEnabled()");
        context.startService(new Intent(context, Time.class));
    }

    @Override
    public void onDisabled(Context context) {
        Log.d(TAG, "onDisabled()");
        context.stopService(new Intent(context, Time.class));
        
    }

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		// When the user deletes the widget, delete the preference associated with it.
		Log.d(TAG, "onDeleted()");
		TxClckWidget widget = new TxClckWidget(context);
		for(int id : appWidgetIds) {
			if(widget.load(id)) {
				widget.deleteWidget();
			}
		}
	}



	static public void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
		Log.d(TAG, "updateWidget() id => "+appWidgetId);
		
		TxClckWidget widget = new TxClckWidget(context);
		
		if(widget.load(appWidgetId) ) {
			
			RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.txclck_layout);
			TxClckTime tm = new TxClckTime();

			views.setTextViewText(R.id.hours, (widget._format.contains("4")) ? tm.getHours(1) : tm.getHours(0) );

			//setting the descriptions
			if(!widget._format.contains("4")) {
				views.setTextViewText(R.id.am, tm.getDesc());
			}else {
				views.setTextViewText(R.id.am, "");
			}

			//setting the minutes 
			views.setTextViewText(R.id.minutes, tm.getMinutes());


			//setting the Font Color for the Widget
			if(widget._flcolor != null)
				views.setTextColor(R.id.hours, Color.parseColor("#"+widget._flcolor));
			if(widget._ftcolor != null)
				views.setTextColor(R.id.minutes, Color.parseColor("#"+widget._ftcolor));
			if(widget._fbcolor != null)
				views.setTextColor(R.id.am, Color.parseColor("#"+widget._fbcolor));

			//setting the Background Color, only for android 2.2 aka froyo
			if(VERSION.SDK_INT >= 8) {
				views.setInt(R.id.linlay, "setBackgroundColor", Color.parseColor("#"+widget._bcolor));
			}


			//setting the pending intent
			try {
				//setting the OnClick Pending INtent for the whole Layout
				String appname;
				if((appname = widget._appname) != null || widget._appname != "null") {
					Intent intent = new Intent(Intent.ACTION_MAIN);
					intent.setComponent(ComponentName.unflattenFromString(appname));
					PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
					views.setOnClickPendingIntent(R.id.linlay, pendingIntent);
				}else if((appname = widget._appname) == "update") {
					Intent inten = new Intent(TxWidgetsFinals.TXCLCK_UPDATE);
					PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, inten, PendingIntent.FLAG_UPDATE_CURRENT);
					views.setOnClickPendingIntent(R.id.linlay, pendingIntent);
				}
			}catch(Exception e) {
				
			}
			// Tell the AppWidgetManager to perform an update on the current App Widget
			appWidgetManager.updateAppWidget(appWidgetId, views);
		}
	}


	public static class Time extends Service {

		private static String TAGSERVICE ="tmi.li.txwidgets.txclck.TxClckProvider$Time";
		private TxClckReceiver mReceiver;
		
		@Override
		public void onCreate() {
			Log.d(TAGSERVICE, "onCreate()");
			mReceiver = new TxClckReceiver();
			this.registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
		}
		
		
		@Override
	    public int onStartCommand(Intent intent, int flags, int startId) 
	    {
	    	Log.d(TAGSERVICE, "onStartCommand()");	        
 
	        // We want this service to continue running until it is explicitly
	        // stopped, so return sticky.
	        return START_STICKY;
	    }
		
		@Override
		public void onDestroy() {
			Log.d(TAGSERVICE, "onDestroy()");
			// TODO Auto-generated method stub
			this.unregisterReceiver(mReceiver);
			super.onDestroy();
		}

		@Override
		public IBinder onBind(Intent intent) {
			return null;
		}   
	}			
}